class Solution
{
public:
    ListNode* reverseKGroup(ListNode* head, int k)
    {
        auto node = head;
        for (int i = 0; i < k; i++)
        {
            if (!node) return head;
            node = node->next;
        }
        auto res = reverse(head, node);
        head->next = reverseKGroup(node, k);
        return res;
    }

private:
    ListNode* reverse(ListNode* l, ListNode* r)
    {
        auto pre = r;
        while (l != r)
        {
            auto node = l->next;
            l->next = pre;
            pre = l, l = node;
        }
        return pre;
    }
};